home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 239_01 / money.c < prev    next >
Text File  |  1987-07-29  |  10KB  |  369 lines

  1.  
  2. /* money.c, david c. oshel, 11/22/86
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <time.h>
  9. #include <malloc.h>
  10. #include <stdarg.h>
  11. #include <signal.h>
  12. #include <setjmp.h>
  13. #include "ciao.h"
  14.  
  15. #define ESC 27
  16. #define MINUS '-'
  17.  
  18. /* edit interrupt flags
  19. */
  20.  
  21. int ESCflag = 0, MINUSflag = 0;
  22.  
  23.  
  24. /* system date
  25. */
  26.  
  27. char date_mask[] = "00/00/00";   /* initialized from system date */
  28.  
  29.  
  30. /*
  31. **  dlrcvt -- return a pointer to dollar & cents string with commas
  32. **            input is a long value
  33. **            has trailing minus sign if negative input
  34. **            dollar sign not included
  35. **            leading spaces not included
  36. **            overflow shows ***,***,***.**
  37. */
  38.  
  39. char *dlrcvt( dval )
  40. long dval;
  41. {
  42.         register char *f, *q;
  43.         static char *p, dollars[16];
  44.         auto char buffer[34];
  45.         auto int sign;
  46.  
  47.         /* initialize receiving field */
  48.  
  49.                       /*  0....v....1....v */
  50.         strcpy( dollars, "   ,   ,  0.00 "); 
  51.  
  52.         /* fetch digits to be formatted */
  53.  
  54.         p = ltoa( dval, buffer, 10 );
  55.  
  56.         sign = (*p == '-');
  57.         if (sign) ++p;
  58.  
  59.  
  60.         /* format dollars field, right to left */
  61.  
  62.         q = strchr(p,'\0');        /* locate from end of source string */
  63.         q--;
  64.         f = dollars + 13;          /* locate to end of destination */
  65.         while (f >= dollars)
  66.         {
  67.              if ( q >= p )   /* is q to the right of first digit? */
  68.              {
  69.                  if (*f == ',' || *f == '.') --f;  /* jump left */
  70.                  *f = *q--;
  71.              }
  72.              else            /* no, no more digits remain in source */
  73.              {
  74.                  if (f < dollars + 10) 
  75.                  {
  76.                      *f = ' ';  /* fill destination left with blanks */
  77.                  }
  78.              }
  79.              --f;
  80.         }
  81.  
  82.         if (q > buffer)  /* OVERFLOW? does source still have digits? */
  83.         {
  84.                            /*  0....v....1....v */
  85.              strcpy( dollars, "***,***,***.** ");  /* overflow */
  86.         }
  87.  
  88.         if (sign) dollars[14] = '-';
  89.         f = dollars;
  90.         while (*f == ' ') f++;
  91.         return (f);
  92.  
  93. } /* dlrcvt() */
  94.  
  95.  
  96. /* Same, but minus sign is interpreted as trailing Cr, positive is Dr
  97. */
  98.  
  99. char *crdrform( x )
  100. long x;
  101. {
  102.      static char *q, r[20];
  103.  
  104.      strcpy( r, dlrcvt( x ) );
  105.      q = strchr( r, '\0' );
  106.      --q;
  107.      if ( x == 0L )      strcpy( q, "Dr" ); /* zero is positive */
  108.      else if (*q == '-') strcpy( q, "Cr" ); 
  109.      else                strcpy( q, "Dr" );
  110.      return (r);
  111. }
  112.  
  113.  
  114.  
  115. /* keyin with key click and Control C trap
  116. ** user must do  BREAKOFF()  in order to enable Control C trapping
  117. ** the macro is defined in "ciao.h"
  118. ** BREAK becomes ESC, but screen is still marred by "^C" message
  119. */
  120.  
  121. static jmp_buf l_onj;
  122. static void c_onj()
  123. {
  124.      longjmp( l_onj, -1 );
  125. }
  126.  
  127. int getkey()
  128. {
  129.       int n;
  130.       signal( SIGINT, c_onj );
  131.       if (setjmp( l_onj ) == 0) n = keyin( noop ); else n = ESC;
  132.       signal( SIGINT, SIG_DFL );
  133.       HIclack();
  134.       return (n);
  135. }
  136.  
  137.  
  138. /* edit masked numeric
  139. ** may only edit digits in the self-masking string, can't delete
  140. ** skips over (forward or backward) any edit character not a digit
  141. ** calls Function Key handler if user enters one
  142. ** the called Fn function is responsible for screen condition!
  143. ** returns 0 if successful 
  144. ** returns ESC if user interrupts with ESC and ESCflag is non-zero
  145. */
  146.  
  147. int edit_masked_numeric( buffptr, fnkey )
  148. char *buffptr;
  149. void (* fnkey)();
  150. {
  151.      register char *p; 
  152.      register int ch;
  153.      int len;
  154.  
  155.      /* highlight string to be edited */
  156.  
  157.      wprintf( "^2%s", buffptr );
  158.      len = strlen( buffptr );
  159.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  160.  
  161.      p = buffptr;
  162.      while (*p)
  163.      {
  164.          if ( isdigit( *p ) ) 
  165.          {
  166.               odd:  ch = getkey();
  167.               switch (ch)
  168.               {
  169.               case ESC:{ if (ESCflag != 0) return (ESC); break; }
  170.               case 13: { /* accept entry */
  171.                        while (*p) wink( *p++ );
  172.                        continue;
  173.                        break;
  174.                        }
  175.               case '\b':
  176.               case 203:{ /* left arrow */
  177.                        if (p > buffptr)
  178.                        {
  179.                           do
  180.                           {
  181.                              --p;
  182.                              wink( '\b' );
  183.                           }
  184.                           while ( (!isdigit( *p )) && (p > buffptr) );
  185.                        }
  186.                        continue;
  187.                        break;
  188.                        }
  189.               case 205:{ /* right arrow */
  190.                        wink( *p++ );
  191.                        continue;
  192.                        break;
  193.                        }
  194.               case 187:
  195.               case 188:
  196.               case 189:
  197.               case 190:
  198.               case 191:  /* was input a Fn key? */
  199.               case 192:
  200.               case 193:
  201.               case 194:
  202.               case 195:
  203.               case 196:{
  204.                        (* fnkey)( ch );
  205.                        continue;
  206.                        break;
  207.                        }
  208.               default: if ( isascii( ch ) && isdigit( ch ) )
  209.                        ;
  210.                        else { thurb(); goto odd; }
  211.               }
  212.          }
  213.          else ch = *p;        /* not digit mask, accept entry from buffptr */
  214.  
  215.          *p = ch;             /* put char in buffptr */
  216.          wink( *p++ );        /* echo it */
  217.  
  218.      }
  219.  
  220.      /* display string as edited, normal vid mode */
  221.  
  222.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  223.      wprintf( "^0%s", buffptr );
  224.      return ( 0 );
  225. }
  226.  
  227.  
  228.  
  229.  
  230. /* Edit String.
  231. ** calls Function Key handler if user enters one
  232. ** the called Fn function is responsible for screen condition!
  233. ** returns ESC if user interrupts with ESC and ESCFlag is nonzero
  234. ** returns MINUS if user interrupts with MINUS and MINUSFlag is nonzero
  235. ** returns 0 otherwise 
  236. */
  237.  
  238. int edit_string( buffptr, fnkey )
  239. char *buffptr;
  240. void (* fnkey)();
  241. {
  242.      register char *p; 
  243.      register int ch;
  244.      int len;
  245.  
  246.      /* highlight string to be edited */
  247.  
  248.      wprintf( "^2%s", buffptr );
  249.      len = strlen( buffptr );
  250.      for (ch = 0; ch < len; ch++ ) wink( '\b' );
  251.  
  252.      p = buffptr;
  253.      while (*p)
  254.      {
  255.           ch = getkey();
  256.           if ( isascii( ch ) )
  257.           {
  258.              switch (ch)
  259.              {
  260.              case ESC:{ if (ESCflag != 0) return (ESC); break; }
  261.              case '\r':{
  262.                       /* CR, accept input & exit */
  263.                       while (*p) wink( *p++ );
  264.                       break;
  265.                       }
  266.              case '\b':{                        /* backspace */
  267.                       if (p > buffptr)
  268.                       {
  269.                          --p;
  270.                          wink( '\b' );
  271.                       }
  272.                       break;
  273.                       }
  274.              case '-':{ if (MINUSflag != 0) return (MINUS); /* NO BREAK */ } 
  275.              default: {
  276.                       if ( isgraph( ch ) || ch == 0x20 ) 
  277.                       { 
  278.                           *p = ch;              /* put char in buffptr */
  279.                           wink( *p++ );         /* echo it */
  280.                       }
  281.                       else 
  282.                       {
  283.                           thurb();              /* reject unprintables */
  284.                       }
  285.                       break;
  286.                       }
  287.              }
  288.           }
  289.           else
  290.           {
  291.              switch (ch)
  292.              {
  293.              case 187:
  294.              case 188:
  295.              case 189:
  296.              case 190:
  297.              case 191:  /* was input a function key? */
  298.              case 192:
  299.              case 193:
  300.              case 194:
  301.              case 195:
  302.              case 196:{
  303.                       (* fnkey)( ch );
  304.                       break;
  305.